/*
 * Copyright 2020 www.mytijian.com All right reserved. This software is the
 * confidential and proprietary information of www.mytijian.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with www.mytijian.com.
 */
package com.mytijian.task.task.listener;

import com.google.common.collect.Lists;
import com.mytijian.task.enums.TaskStatusEnum;
import com.mytijian.task.task.TaskIdentifyOperateService;
import com.mytijian.task.model.TaskIdentify;
import com.mytijian.task.model.TaskValue;

/**
 *
 */
public class FailoverTaskIndentifyListener implements RuningTaskListener {
    private Integer hashCode = 1;
    private TaskIdentifyOperateService taskIdentifyOperateService;
    public FailoverTaskIndentifyListener(TaskIdentifyOperateService taskIdentifyOperateService) {
        this.taskIdentifyOperateService = taskIdentifyOperateService;
    }
    @Override
    public boolean newTaskIdentify(TaskIdentify taskIdentify, TaskValue taskValue) {
        if (TaskStatusEnum.getUnProcessStatusInteger().contains(taskValue.getTaskStuts())) {
            return true;
        }
        return taskIdentifyOperateService.submit(taskIdentify, TaskStatusEnum.getByCode(taskValue.getTaskStuts()));
    }

    @Override
    public void removeRemoteTaskIdentify(TaskIdentify taskIdentify) {
        taskIdentifyOperateService.clear(Lists.newArrayList(taskIdentify));

    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof FailoverTaskIndentifyListener)) return false;

        FailoverTaskIndentifyListener that = (FailoverTaskIndentifyListener) o;

        return hashCode.equals(that.hashCode);
    }

    @Override
    public int hashCode() {
        return hashCode.hashCode();
    }
}
